package com.transaction.manage.util;

import com.transaction.common.util.FileUtil;
import com.transaction.manage.localMapper.InterfaceFieldMapper;
import com.transaction.manage.model.dto.InterfaceField;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class BuildSqlUtil {

    @Autowired
    private InterfaceFieldMapper interfaceFieldMapper;

    public void buildCreateTableSql(String tableName,String interfaceCode){

        StringBuilder createTableSql = new StringBuilder();
        createTableSql.append("CREATE TABLE `").append(tableName).append("` (").append("\r\n")
                .append("`id` int(11) NOT NULL AUTO_INCREMENT,").append("\r\n")
                .append("`channel_code` varchar(50) NOT NULL COMMENT '渠道编码',").append("\r\n")
                .append("`handle_date` varchar(10) NOT NULL COMMENT '处理日期',").append("\r\n");

        InterfaceField interfaceField = new InterfaceField(interfaceCode);
        //1、读取字段信息
        List<InterfaceField> interfaceFieldList = interfaceFieldMapper.select(interfaceField);
        for (InterfaceField field : interfaceFieldList) {
            //2、根据字段信息进行组装建表语句
            String enName = field.getColEnName();
            String comment = field.getColCnName();
            String colType = field.getColType();
            Integer length = field.getColLength();
            Integer decimal = field.getColDecimal();
            Integer required = field.getColRequired();

            createTableSql.append("`").append(camel4underline(enName)).append("`    ");
            if("A".equals(colType) || "C".equals(colType)){
                createTableSql.append("varchar(").append(length*2).append(") ");
            }else{
                createTableSql.append("decimal(").append(length).append(",").append(decimal).append(") ");
            }
            if(1 == required){
                createTableSql.append("NOT NULL ");
            }

            if("N".equals(colType)){
                createTableSql.append(" DEFAULT 0.00 ");
            }

            createTableSql.append("COMMENT '").append(comment).append("',").append("\r\n");
        }

        createTableSql.append("`flow_step` text NOT NULL COMMENT '流程步骤过程记录',").append("\r\n")
                .append("`utime` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),").append("\r\n")
                .append("PRIMARY KEY (`id`)").append("\r\n")
                .append(") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
        System.out.println(createTableSql);
        //3、把组装的语句写入文件

        FileUtil.writeFile(new File("D:\\sql.sql"),createTableSql.toString());

    }

    /**
     * 大写字母前加_并转小写
     * @param param
     * @return
     */
    public static String camel4underline(String param){

        if(StringUtils.isEmpty(param)){
            return "";
        }

        Pattern p = Pattern.compile("[A-Z]");
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);

        int i = 0;
        while(mc.find()){
            builder.replace(mc.start() + i,mc.end() + i,"_" + mc.group().toLowerCase());
            i++;
        }
        if('_' == builder.charAt(0)){
            builder.deleteCharAt(0);
        }

        return builder.toString();

    }
}
